package com.dbq.fast.business.product.adapter;

import android.content.res.Resources;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dbq.fast.R;
import com.dbq.fast.business.product.bean.ProductType;
import com.yiba.vpn.protocal.dns.Resource;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 */

public class ProductListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ProductType.SetMealListBean> data;
    private OnCheckProductListener listener;

    private int VIEW_TITLE = 1;//title
    private int VIEW_CONTENT = 2;//content
    private int VIEW_PRICE = 3;//price

    public ProductListAdapter(List<ProductType.SetMealListBean> data) {
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_PRICE) {
            View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_product_item_view, null);
            return new ProductViewHolder(convertView);
        } else if (viewType == VIEW_CONTENT) {
            View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_product_content_detail_view, null);
            return new ContentViewHolder(convertView);
        } else if (viewType == VIEW_TITLE) {
            View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_product_title_view, null);
            return new TitleViewHolder(convertView);
        } else {
            return null;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).VIEW_TYPE;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (getItemViewType(position) == VIEW_PRICE) {
            if (holder instanceof ProductViewHolder) {
                normalView((ProductViewHolder) holder, position);
            }
        } else if (getItemViewType(position) == VIEW_CONTENT) {
            if (holder instanceof ContentViewHolder) {
                contentView((ContentViewHolder) holder, position);
            }
        } else if (getItemViewType(position) == VIEW_TITLE) {
            if (holder instanceof TitleViewHolder) {
                titleView((TitleViewHolder) holder, position);
            }
        }
    }

    private void normalView(ProductViewHolder holder, int position) {
        ProductType.SetMealListBean itemData = data.get(position);
        Resources resource = holder.itemView.getContext().getResources();
        holder.timeTv.setText(String.valueOf(itemData.setMealTime) + resource.getString(R.string.mouths));
        if (TextUtils.isEmpty(itemData.discounts)) {
            holder.discountRateTv.setVisibility(View.INVISIBLE);
        } else {
            holder.discountRateTv.setVisibility(View.VISIBLE);
            holder.discountRateTv.setText(resource.getString(R.string.save) + itemData.discounts);
        }

        holder.priceTv.setText("¥" + itemData.unitPrice);
        holder.timeUnitTv.setText("/" + resource.getString(R.string.mouth));
        if (itemData.isInvalid) {
            holder.productRootRl.setBackgroundResource(R.drawable.unchoose_item);
            holder.timeTv.setTextColor(resource.getColor(R.color.load_fail));
            holder.priceTv.setTextColor(resource.getColor(R.color.load_fail));
            holder.timeUnitTv.setTextColor(resource.getColor(R.color.load_fail));
        } else {

            holder.timeTv.setTextColor(resource.getColor(R.color.font_title));
            holder.priceTv.setTextColor(resource.getColor(R.color.font_title));
            holder.timeUnitTv.setTextColor(resource.getColor(R.color.font_title));

            holder.productRootRl.setOnClickListener(v -> {
                for (ProductType.SetMealListBean i : data) {
                    if (i == itemData) {
                        itemData.isChecked = true;

                    } else {
                        i.isChecked = false;
                    }
                    notifyDataSetChanged();
                }
            });
            if (itemData.isChecked) {
                holder.productRootRl.setBackgroundResource(R.drawable.choose_item);
                if (listener != null) {
                    listener.checkProduct(position, itemData);
                }
            } else {
                holder.productRootRl.setBackgroundResource(R.drawable.unchoose_item);
            }
        }

    }

    private void titleView(TitleViewHolder holder, int position) {
        ProductType.SetMealListBean itemData = data.get(position);
        holder.adStatusTv.setText(itemData.ad);
        holder.speedTv.setText(itemData.speed);
        holder.titleTv.setText(itemData.title);
        if (TextUtils.isEmpty(itemData.image)) {
            holder.imageLl.setVisibility(View.INVISIBLE);
        } else {
            holder.imageLl.setVisibility(View.VISIBLE);
            holder.tv4k.setText(itemData.image);
        }

        holder.videoTv.setText(itemData.video);
        holder.llOther.setOnClickListener(v -> {
            if (data.get(1).isVisible) {
                data.get(1).isVisible = false;
            } else {
                data.get(1).isVisible = true;
            }
            notifyDataSetChanged();
        });
    }

    private void contentView(ContentViewHolder holder, int position) {
        ProductType.SetMealListBean itemData = data.get(position);
        String desc = itemData.content.replace("\\n", "\n");
        holder.hintDetailTv.setText(desc);
        Log.i("itemData.isVisible", "itemData.isVisible=" + itemData.isVisible);
        holder.rlRoot.setVisibility(itemData.isVisible ? View.VISIBLE : View.GONE);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    static class ProductViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.product_item_view)
        RelativeLayout productRootRl;

        @BindView(R.id.time)
        TextView timeTv;


        @BindView(R.id.tv_discount_rate)
        TextView discountRateTv;

        @BindView(R.id.time_unit)
        TextView timeUnitTv;

        @BindView(R.id.price)
        TextView priceTv;


        public ProductViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    static class TitleViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_title)
        TextView titleTv;
        @BindView(R.id.tv_ad_status)
        TextView adStatusTv;
        @BindView(R.id.tv_video)
        TextView videoTv;
        @BindView(R.id.tv_speed)
        TextView speedTv;
        @BindView(R.id.ll_image)
        LinearLayout imageLl;
        @BindView(R.id.tv_4k)
        TextView tv4k;
        @BindView(R.id.other)
        ImageView other;

        @BindView(R.id.ll_other)
        LinearLayout llOther;

        public TitleViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class ContentViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_hint_detail)
        TextView hintDetailTv;
        @BindView(R.id.rl_root)
        RelativeLayout rlRoot;

        public ContentViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public void setOnCheckProductListener(OnCheckProductListener listener) {
        this.listener = listener;
    }

    public interface OnCheckProductListener {
        void checkProduct(int index, ProductType.SetMealListBean data);
    }

}
